{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0813 22:41:31.646412 140564447225664 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:153: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "from keras.models import load_model\n",
    "from keras import backend as K\n",
    "K.set_learning_phase(1)\n",
    "\n",
    "def build_model():\n",
    "    #加载模型\n",
    "    model = load_model('keras_photo_mutiply_n.h5')\n",
    "    return model\n",
    "\n",
    "def save_model_for_production(save_path,model):\n",
    "    #定义模型输入输出\n",
    "    \n",
    "    builder = tf.saved_model.builder.SavedModelBuilder(save_path)\n",
    "\n",
    "    input0 = tf.saved_model.utils.build_tensor_info(model.input[0])\n",
    "    input1 = tf.saved_model.utils.build_tensor_info(model.input[1])\n",
    "    model_output = tf.saved_model.utils.build_tensor_info(model.output)\n",
    "\n",
    "    prediction_signature = (\n",
    "        tf.saved_model.signature_def_utils.build_signature_def(\n",
    "            inputs={'photo': input0, 'number': input1},\n",
    "            outputs={'output': model_output},\n",
    "            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))\n",
    "\n",
    "    with K.get_session() as sess:\n",
    "        builder.add_meta_graph_and_variables(\n",
    "            sess=sess, tags=[tf.saved_model.tag_constants.SERVING],\n",
    "            signature_def_map={\n",
    "                'photo_mutiply_number':\n",
    "                    prediction_signature,\n",
    "            })\n",
    "\n",
    "        builder.save()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0813 21:35:16.078784 140276744169280 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
      "\n",
      "W0813 21:35:16.103199 140276744169280 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
      "\n",
      "W0813 21:35:16.162461 140276744169280 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.\n",
      "\n",
      "W0813 21:35:16.163149 140276744169280 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n",
      "\n",
      "W0813 21:35:16.932024 140276744169280 deprecation_wrapper.py:119] From /home/fyyc/anaconda3/envs/tf-env/lib/python3.6/site-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n",
      "\n",
      "W0813 21:35:17.450030 140276744169280 deprecation.py:323] From <ipython-input-1-3247463fb7f2>:17: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor_info or tf.compat.v1.saved_model.build_tensor_info.\n"
     ]
    }
   ],
   "source": [
    "model = build_model()\n",
    "save_path = './saved_model'\n",
    "save_model_for_production(save_path,model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf-env",
   "language": "python",
   "name": "tf-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
